<template>
	<view class="address">
		<radio-group>
			<label class="list" v-for="item in res" :key="res.id">
				<view class="cont">
					<view class="inner">
						<view class="title">
							<text class="realname" v-cloak>{{item.realname}}</text>
							<text class="mobile" v-cloak>{{item.mobile}}</text>
						</view>
						<view class="text" v-cloak>{{item.province}}{{item.city}}{{item.area}}&nbsp;&nbsp;{{item.address}}</view>
						<view class="bar clear">
							<view class="pull-right webkit">
								<!-- <view class="flex"></view> -->
								<view @tap="jump" :data-id="item.id">
									<image src="../../../static/edit.png"></image>
									<text>编辑</text>
								</view>
								<view @tap='del' :data-id="item.id">
									<image src="../../../static/delete.png"></image>
									<text>删除</text>
								</view>
							</view>
							<view class="radio-inline" @click="moren" :data-id="item.id">
								<radio :value="item.id" :checked="item.isdefault" color="#f55"/><text style="position: relative;top: 4upx;">设为默认</text>
							</view>
						</view>
					</view>
				</view>
			</label>
		</radio-group>
		<view class="footer">
			<button type="primary" @click="jump">+ 新增地址</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				res:[],
				openid:''
			};
		},
		onLoad:function(){
			uni.showLoading()
			uni.getStorage({
				key: 'openid',
				success: (res) => {
					this.openid = res.data
					this.setAjax()
				},
				fail: () => {
					uni.navigateTo({
						url: '../../login/login'
					});
				}
			});
		},
		onPullDownRefresh() {
			this.setAjax();
		},
		methods: {
			radioChange: function (e) {
				uni.showLoading()
				uni.request({
					url: 'https://app.t9y9y9.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=member.address.setdefault&app=1&id='+e.detail.value,
					method: 'POST',
					data: {'openid':this.openid},
					header:{'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						uni.hideLoading()
						uni.showToast({
							title: "设置成功"
						})
						var items = this.res;
						for (var i = 0, len = items.length; i < len; ++i) {
							items[i].checked = items[i].value == e.detail.value
						}
						this.res = items
					},
					fail: () => {},
					complete: () => {}
				});
			},
			setAjax(){
				uni.request({
					url: 'https://app.t9y9y9.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=member.address&app=1',
					method: 'POST',
					data: {'openid':this.openid},
					header:{'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						uni.hideLoading()
						uni.stopPullDownRefresh();
						for(var i in res.data){
							if(res.data[i].isdefault == 0){
								res.data[i].isdefault = false
							}else{
								res.data[i].isdefault = true
							}
						}
						this.res = res.data
					},
					fail: () => {},
					complete: () => {}
				});
				
			},
			jump(e){
				 // event.stopPropagation();
				 uni.navigateTo({
				 	url: 'post?id='+e.currentTarget.dataset.id
				 });
			},
			del(e){
				 // event.stopPropagation();
				 uni.showModal({
					content: "是否删除地址",
					confirmText: "确定",
					cancelText: "取消",
				 	success: res => {
						if(res.confirm){
							uni.showLoading()
							uni.request({
								url: 'https://app.t9y9y9.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=member.address.delete&app=1&id='+e.currentTarget.dataset.id,
								method: 'POST',
								data: {'openid':this.openid},
								header:{'Content-Type':'application/x-www-form-urlencoded'},
								success: res => {
									uni.hideLoading()
									uni.showToast({
										title: "删除成功"
									})
									setTimeout(() => {
										uni.showLoading()
										this.setAjax()
									}, 500);
								},
								fail: () => {},
								complete: () => {}
							});
							
						}
					},
				 	fail: () => {},
				 	complete: () => {}
				 });
			},
			moren(e){
				uni.showLoading()
				uni.request({
					url: 'https://app.t9y9y9.com/app/index.php?i=4&c=entry&m=ewei_shopv2&do=mobile&r=member.address.setdefault&app=1&id='+e.currentTarget.dataset.id,
					method: 'POST',
					data: {'openid':this.openid},
					header:{'Content-Type':'application/x-www-form-urlencoded'},
					success: res => {
						uni.hideLoading()
						this.setAjax()
					},
					fail: () => {},
					complete: () => {}
				});
			}
		}
	}
</script>
<style>.list{padding:0;display:block;padding-top:10upx;}.cont{background:#fff;}.inner{padding:16upx 24upx;}.title{font-size:28upx;color:#000;white-space:nowrap;text-overflow:ellipsis;overflow:hidden;height:40upx;line-height:40upx;}.realname{margin-right:10upx;}.text{font-size:26upx;color:#666;}.bar{position:relative;color:#666;margin-top:12upx;padding-top:8upx;line-height:56upx;}.bar:before{content:" ";position:absolute;left:0;top:0;width:100%;height:2upx;border-top:2upx solid #ebebeb;color:#ebebeb;-webkit-transform-origin:0 0;-ms-transform-origin:0 0;transform-origin:0 0;-webkit-transform:scaleY(0.5);-ms-transform:scaleY(0.5);transform:scaleY(0.5);}.pull-right{float:right;}.pull-right view{margin-left:20upx;}.pull-right view image{width:34upx;height:34upx;position:relative;top:6upx;}.footer{position:fixed;bottom:0;width:100%;}.footer button{border-radius:0;background:#f55;}.footer button.button-hover{background:#dc4d4d;}</style>